<?php

namespace app\controller\admin\genshinteam;

use app\model\api\genshin\GenshinArea;
use app\model\api\GenshinCategory;
use app\model\api\GenshinRelics;
use app\model\api\GenshinRole;
use app\model\api\GenshinWeapon;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use laytp\library\Tree;
use think\facade\Db;

/**
 * 菜单控制器
 */
class Role extends Backend
{

    public $model;
    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public function _initialize()
    {
        $this->model = new GenshinRole();
    }

    public function role()
    {
        $data = $this->model->getRoleList();
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());

        $post['position']=strtr($post['position'],',','/');
        $post['tag']=strtr($post['tag'],'，',',');
        $insData['suoyin'] = '';
        $insData['uid'] = '';
        $insData['role_name'] = $post['name'];
        $insData['en_alias'] = $post['en_alias'];
        $insData['icon'] = $post['icon'];
        $insData['head_img'] = $post['head_img'];
        $insData['birthday'] = $post['birthday'];
        $insData['title'] = $post['title'];
        $insData['stand'] = $post['stand'];
        $insData['alias'] = $post['alias'];
        $insData['element'] = $post['element'];
        $insData['star'] = $post['star'];
        $insData['nation'] = (new GenshinArea())->where('name',$post['nation'])->value('en_name');
        $insData['position'] = $post['position'];
        $insData['suoshu'] = $post['suoshu'];
        $insData['tag'] = $post['tag'];
        $insData['mingzuo'] = $post['mingzuo'];
        $insData['weapon'] = $post['weapon'];
        $insData['breach'] = $post['breach'];
        $insData['introduce'] = $post['introduce'];
        $insData['comment'] = $post['comment'];
        $insData['wanfa'] = $post['position'];
        //六边形数据
        $wuwei = json_encode([$post['num1'], $post['num2'], $post['num3'], $post['num4'], $post['num5'], $post['num6']], JSON_UNESCAPED_UNICODE);
        $insData['wuwei'] = $wuwei;

        //技能
        $skill1 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name1'], 'icon' => $post['skill_img1']);
        $skill2 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name3'], 'icon' => $post['skill_img3']);
        $skill3 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name2'], 'icon' => $post['skill_img2']);
        $insData['skill'] = json_encode(array($skill1, $skill2, $skill3), JSON_UNESCAPED_UNICODE);

        //推荐武器
        $recommend_weapon = [];
        $weaponList = [];
        $weapon = (new GenshinWeapon())->select()->toArray();
        foreach ($weapon as $index => $item) {
            $weaponList[$item['name']] = $item;
        }
        $post['recommend_weapon'] = explode(',', $post['recommend_weapon']);
        foreach ($post['recommend_weapon'] as $i => $k) {
            if (empty($k)) {
                continue;
            }

            $recommend_weapon[] = array('weapon_id' => '', 'name' => $k, 'rarity' => $weaponList[$k]['rarity'], 'type_name' => $weaponList[$k]['type_name'],'id'=>(new GenshinWeapon())->where('name',$k)->value('id'));
        }
        $insData['recommend_weapon'] = json_encode($recommend_weapon, JSON_UNESCAPED_UNICODE);

        //推荐圣遗物
        $recommend_relics1 = [];
        $post['recommend_relics1'] = explode(',', $post['recommend_relics1']);
        foreach ($post['recommend_relics1'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics1']) == 2) {
                $recommend_relics1[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics1']) == 1) {
                $recommend_relics1[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $recommend_relics2 = [];
        $post['recommend_relics2'] = explode(',', $post['recommend_relics2']);
        foreach ($post['recommend_relics2'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics2']) == 2) {
                $recommend_relics2[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics2']) == 1) {
                $recommend_relics2[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $recommend_relics3 = [];
        $post['recommend_relics3'] = explode(',', $post['recommend_relics3']);
        foreach ($post['recommend_relics3'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics3']) == 2) {
                $recommend_relics3[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics3']) == 1) {
                $recommend_relics3[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $relicsList=[];
        if (!empty($recommend_relics1)){
            array_push($relicsList,$recommend_relics1);
        }
        if (!empty($recommend_relics2)){
            array_push($relicsList,$recommend_relics2);
        }
        if (!empty($recommend_relics3)){
            array_push($relicsList,$recommend_relics3);
        }
        $insData['recommend_relics'] = json_encode($relicsList, JSON_UNESCAPED_UNICODE);
        //推荐队伍
        $role = (new GenshinRole())->select()->toArray();
        $roleList = [];
        foreach ($role as $i => $k) {
            $roleList[$k['role_name']] = $k;
        }
        $teamList1 = [];
        $post['team_role1'] = explode(',', $post['team_role1']);
        foreach ($post['team_role1'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList1[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
       if (sizeof($teamList1)==3){
           array_push($teamList1,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
       }
        $team1 = array('id' => '', 'title' => $post['team_title1'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'), 'teamlist' => $teamList1, 'desc' => '');
        $teamList2 = [];
        $post['team_role2'] = explode(',', $post['team_role2']);
        foreach ($post['team_role2'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList2[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList2)==3){
            array_push($teamList2,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team2 = array('id' => '', 'title' => $post['team_title2'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role2'][0])->value('nation'), 'teamlist' => $teamList2, 'desc' => '');
        $teamList3 = [];
        $post['team_role3'] = explode(',', $post['team_role3']);
        foreach ($post['team_role3'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList3[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList3)==3){
            array_push($teamList3,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team3 = array('id' => '', 'title' => $post['team_title3'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role3'][0])->value('nation'), 'teamlist' => $teamList3, 'desc' => '');
        $teamList4 = [];
        $post['team_role4'] = explode(',', $post['team_role4']);
        foreach ($post['team_role4'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList4[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList4)==3){
            array_push($teamList4,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team4 = array('id' => '', 'title' => $post['team_title4'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role4'][0])->value('nation'), 'teamlist' => $teamList4, 'desc' => '');
        $teamList5 = [];
        $post['team_role5'] = explode(',', $post['team_role5']);
        foreach ($post['team_role5'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList5[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList5)==3){
            array_push($teamList5,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team5 = array('id' => '', 'title' => $post['team_title5'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role5'][0])->value('nation'), 'teamlist' => $teamList5, 'desc' => '');
        $teamList=[];
        if (!empty($team1['teamlist'])){
            array_push($teamList,$team1);
        }
        if (!empty($team2['teamlist'])){
            array_push($teamList,$team2);
        }
        if (!empty($team3['teamlist'])){
            array_push($teamList,$team3);
        }
        if (!empty($team4['teamlist'])){
            array_push($teamList,$team4);
        }
        if (!empty($team5['teamlist'])){
            array_push($teamList,$team5);
        }
        $insData['recommend_team'] = json_encode($teamList, JSON_UNESCAPED_UNICODE);
        //主词条推荐
        $main_entry = [
            array($post['main_entry1']),
            array($post['main_entry2']),
            array($post['main_entry3']),
            array($post['main_entry4']),
            array($post['main_entry5']),
        ];
        $insData['main_entry'] = json_encode($main_entry, JSON_UNESCAPED_UNICODE);
        //副词条推荐
        $sub_entry = [];
        $post['sub_entry'] = explode(',', $post['sub_entry']);
        foreach ($post['sub_entry'] as $i => $k) {
            if (empty($k)){
                continue;
            }
            $sub_entry[] = $k;
        }
        $insData['sub_entry'] = json_encode($sub_entry, JSON_UNESCAPED_UNICODE);
        //加点推荐
        $add_point_recommed = [];
        if (!empty($post['add_point_recommed1'])){
            array_push($add_point_recommed,$post['add_point_recommed1']);
        }
        if (!empty($post['add_point_recommed2'])){
            array_push($add_point_recommed,$post['add_point_recommed2']);
        }
        if (!empty($post['add_point_recommed3'])){
            array_push($add_point_recommed,$post['add_point_recommed3']);
        }
        $insData['add_point_recommed'] = json_encode($add_point_recommed, JSON_UNESCAPED_UNICODE);
        //命座推荐
        $core_mingzuo = [];
        if (!empty($post['core_mingzuo1'])){
            array_push($core_mingzuo,$post['core_mingzuo1']);
        }
        if (!empty($post['core_mingzuo2'])){
            array_push($core_mingzuo,$post['core_mingzuo2']);
        }
        if (!empty($post['core_mingzuo3'])){
            array_push($core_mingzuo,$post['core_mingzuo3']);
        }
        if (!empty($post['core_mingzuo4'])){
            array_push($core_mingzuo,$post['core_mingzuo4']);
        }
        $insData['core_mingzuo'] = json_encode($core_mingzuo, JSON_UNESCAPED_UNICODE);
        //毕业面板
        $insData['panel']=json_encode(array(
            'sid'=>'','uid'=>'',
            'shengming'=>$post['shengming'],
            'fangyu'=>$post['fangyu'],
            'baoji'=>$post['baoji'],
            'baoshang'=>$post['baoshang'],
            'gongji'=>$post['gongji'],
            'yuansujingtong'=>$post['yuansujingtong'],
            'yuansujiacheng'=>$post['yuansujiacheng'],
            'yuansuchongneng'=>$post['yuansuchongneng'],
            'qjineng'=>$post['skill_level2'],
            'ejineng'=>$post['skill_level3'],
            'pinga'=>$post['skill_level1'],
        ),JSON_UNESCAPED_UNICODE);
        $insData['panel_num']=json_encode(array(
            'health'=>$post['health'],
            'attack'=>$post['attack'],
            'defense'=>$post['defense'],
            'crit'=>$post['crit'],
            'critDamage'=>$post['critDamage'],
            'elementMastery'=>$post['elementMastery'],
            'elementCharge'=>$post['elementCharge'],
            'elementBonus'=>$post['elementBonus'],
        ),JSON_UNESCAPED_UNICODE);
        try {
            Db::startTrans();
            $role_id = $this->model->insertGetId($insData);
            if (!$role_id) throw new \Exception('保存失败');
            //添加角色對應技能材料
            $res = $this->model->addSkillLevel($role_id);
            if(!$res){
                Db::rollback();
                throw new \Exception('添加角色对应技能材料失败');
            }
            //添加角色對應升級材料
            $res = $this->model->addRoleLevel($role_id);
            if(!$res){
                Db::rollback();
                throw new \Exception('添加角色对应升级材料失败');
            }
            $this->clearCache();
            Db::commit();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');
        $role = $this->model->where('id',$id)->find()->toArray();
        $role['role_name'] = !empty($role['role_name'])?$role['role_name']:'';
        $role['en_alias'] = !empty($role['en_alias'])?$role['en_alias']:'';
        $role['icon'] = !empty($role['role_name'])?$role['icon']:'';
        $role['head_img'] = !empty($role['head_img'])?$role['head_img']:'';
        $role['birthday'] = !empty($role['birthday'])?$role['birthday']:'';
        $role['title'] = !empty($role['title'])?$role['title']:'';
        $role['stand'] = !empty($role['stand'])?$role['stand']:'';
        $role['alias'] = !empty($role['alias'])?$role['alias']:'';
        $role['element'] = !empty($role['element'])?$role['element']:'';
        $role['star'] = !empty($role['star'])?$role['star']:'';
        $e_name=(new GenshinArea())->where('en_name',$role['nation'])->column('name');
        $role['nation'] = !empty($role['nation'])?$e_name:'';
        $role['position']=explode('/',$role['position']);
        $role['position'] = !empty($role['position'])?$role['position']:'';
        $role['suoshu'] = !empty($role['suoshu'])?$role['suoshu']:'';
        $role['tag'] = !empty($role['tag'])?$role['tag']:'';
        $role['mingzuo'] = !empty($role['mingzuo'])?$role['mingzuo']:'';
        $role['weapon'] = !empty($role['weapon'])?$role['weapon']:'';
        $role['breach'] = !empty($role['breach'])?$role['breach']:'';
        $role['introduce'] = !empty($role['introduce'])?$role['introduce']:'';
        $role['comment'] = !empty($role['comment'])?$role['comment']:'';
        $role['wanfa']=json_decode($role['wanfa'],true);
        $wuwei=json_decode($role['wuwei'],true);
        $role['num1'] = !empty($wuwei[0])?$wuwei[0]:0;
        $role['num2'] = !empty($wuwei[1])?$wuwei[1]:0;
        $role['num3'] = !empty($wuwei[2])?$wuwei[2]:0;
        $role['num4'] = !empty($wuwei[3])?$wuwei[3]:0;
        $role['num5'] = !empty($wuwei[4])?$wuwei[4]:0;
        $role['num6'] = !empty($wuwei[5])?$wuwei[5]:0;
        $skill=json_decode($role['skill'],true);
        foreach ($skill as $index=>$item){
            $role['skill_name'.($index+1)]=$item['name'];
            $role['skill_img'.($index+1)]=$item['icon'];
        }
        $role['recommend_weapon'] = !empty($role['recommend_weapon'])?implode(',',array_column(json_decode($role['recommend_weapon'],true),'name')):'';

        foreach (json_decode($role['recommend_relics']) as $index=>$item){
            $role['recommend_relics'.($index+1)] = !empty($role['recommend_relics'])?implode(',',array_column($item,'set_name')):'';
        }
        $team=json_decode($role['recommend_team'],true);
        for ($num=0;$num<5;$num++){
            $role['team_title'.($num+1)]=!empty($team[$num])?$team[$num]['title']:'';
            $role['team_role'.($num+1)]=!empty($team[$num])?implode(',',array_column($team[$num]['teamlist'],'name')):'';
            $role['team_desc'.($num+1)]=!empty($team[$num]['desc'])?$team[$num]['desc']:'';
        }

        $role['sub_entry']=json_decode($role['sub_entry'],true);
        $role['main_entry']=json_decode($role['main_entry'],true);
        $role['main_entry1']=!empty($role['main_entry'][0])?$role['main_entry'][0]:"";
        $role['main_entry2']=!empty($role['main_entry'][1])?$role['main_entry'][1]:"";
        $role['main_entry3']=!empty($role['main_entry'][2])?$role['main_entry'][2]:"";
        $role['main_entry4']=!empty($role['main_entry'][3])?$role['main_entry'][3]:"";
        $role['main_entry5']=!empty($role['main_entry'][4])?$role['main_entry'][4]:"";
        $role['add_point_recommed']=json_decode($role['add_point_recommed'],true);
        $role['add_point_recommed1']=!empty($role['add_point_recommed'][0])?$role['add_point_recommed'][0]:'';
        $role['add_point_recommed2']=!empty($role['add_point_recommed'][1])?$role['add_point_recommed'][1]:'';
        $role['add_point_recommed3']=!empty($role['add_point_recommed'][2])?$role['add_point_recommed'][2]:'';
        $role['core_mingzuo']=json_decode($role['core_mingzuo'],true);
        $role['core_mingzuo1']=!empty($role['core_mingzuo'][0])?$role['core_mingzuo'][0]:'';
        $role['core_mingzuo2']=!empty($role['core_mingzuo'][1])?$role['core_mingzuo'][1]:'';
        $role['core_mingzuo3']=!empty($role['core_mingzuo'][2])?$role['core_mingzuo'][2]:'';
        $role['core_mingzuo4']=!empty($role['core_mingzuo'][3])?$role['core_mingzuo'][3]:'';
        $panel=json_decode($role['panel'],true);
        $role['shengming']=$panel['shengming'];
        $role['fangyu']=$panel['fangyu'];
        $role['baoji']=$panel['baoji'];
        $role['baoshang']=$panel['baoshang'];
        $role['gongji']=$panel['gongji'];
        $role['yuansujingtong']=$panel['yuansujingtong'];
        $role['yuansujiacheng']=$panel['yuansujiacheng'];
        $role['yuansuchongneng']=$panel['yuansuchongneng'];
        $role['skill_level2']=$panel['qjineng'];
        $role['skill_level3']=$panel['ejineng'];
        $role['skill_level1']=$panel['pinga'];
        $panel=json_decode($role['panel_num'],true);
        $role['health']=$panel['health'];
        $role['attack']=$panel['attack'];
        $role['defense']=$panel['defense'];
        $role['crit']=$panel['crit'];
        $role['critDamage']=$panel['critDamage'];
        $role['elementMastery']=$panel['elementMastery'];
        $role['elementCharge']=$panel['elementCharge'];
        $role['elementBonus']=$panel['elementBonus'];

        return $this->success('获取成功', $role);
    }

    //编辑
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $post['position']=strtr($post['position'],',','/');
        $post['tag']=strtr($post['tag'],'，',',');
        $row->role_name = $post['name'];
        $row->suoyin = '';
        $row->uid = '';
        $row->role_name = $post['name'];
        $row->en_alias= $post['en_alias'];
        $row->icon= $post['icon'];
        $row->head_img = $post['head_img'];
        $row->birthday= $post['birthday'];
        $row->title= $post['title'];
        $row->stand= $post['stand'];
        $row->alias = $post['alias'];
        $row->element= $post['element'];
        $row->star= $post['star'];
        $row->nation= (new GenshinArea())->where('name',$post['nation'])->value('en_name');
        $row->position = $post['position'];
        $row->suoshu= $post['suoshu'];
        $row->tag= $post['tag'];
        $row->mingzuo= $post['mingzuo'];
        $row->weapon= $post['weapon'];
        $row->breach= $post['breach'];
        $row->introduce= $post['introduce'];
        $row->comment= $post['comment'];
        //玩法
        $row->wanfa =$post['position'];
        //六边形数据
        $wuwei = json_encode([$post['num1'], $post['num2'], $post['num3'], $post['num4'], $post['num5'], $post['num6']], JSON_UNESCAPED_UNICODE);
        $row->wuwei= $wuwei;

        //技能
        $skill1 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name1'], 'icon' => $post['skill_img1']);
        $skill2 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name2'], 'icon' => $post['skill_img2']);
        $skill3 = array('iid' => '', 'uid' => '', 'sid' => '', 'name' => $post['skill_name3'], 'icon' => $post['skill_img3']);
        $row->skill= json_encode(array($skill1, $skill2, $skill3), JSON_UNESCAPED_UNICODE);

        //推荐武器
        $recommend_weapon = [];
        $weaponList = [];
        $weapon = (new GenshinWeapon())->select()->toArray();
        foreach ($weapon as $index => $item) {
            $weaponList[$item['name']] = $item;
        }
        $post['recommend_weapon'] = explode(',', $post['recommend_weapon']);
        foreach ($post['recommend_weapon'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $recommend_weapon[] = array('weapon_id' => '', 'name' => $k, 'rarity' => $weaponList[$k]['rarity'], 'type_name' => $weaponList[$k]['type_name'],'id'=>(new GenshinWeapon())->where('name',$k)->value('id'));
        }
        $row->recommend_weapon= json_encode($recommend_weapon, JSON_UNESCAPED_UNICODE);

        //推荐圣遗物
        $recommend_relics1 = [];
        $post['recommend_relics1'] = explode(',', $post['recommend_relics1']);
        foreach ($post['recommend_relics1'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics1']) == 2) {
                $recommend_relics1[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics1']) == 1) {
                $recommend_relics1[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $recommend_relics2 = [];
        $post['recommend_relics2'] = explode(',', $post['recommend_relics2']);
        foreach ($post['recommend_relics2'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics2']) == 2) {
                $recommend_relics2[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics2']) == 1) {
                $recommend_relics2[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $recommend_relics3 = [];
        $post['recommend_relics3'] = explode(',', $post['recommend_relics3']);
        foreach ($post['recommend_relics3'] as $ii => $k) {
            if (empty($k)) {
                continue;
            }
            if (count($post['recommend_relics3']) == 2) {
                $recommend_relics3[] = array('name' => '', 'set_name' => $k, 'num' => '2','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            } elseif (count($post['recommend_relics3']) == 1) {
                $recommend_relics3[] = array('name' => '', 'set_name' => $k, 'num' => '4','id'=>(new GenshinRelics())->where('relics_name',$k)->value('id'));
            }
        }
        $relicsList=[];
        if (!empty($recommend_relics1)){
            array_push($relicsList,$recommend_relics1);
        }
        if (!empty($recommend_relics2)){
            array_push($relicsList,$recommend_relics2);
        }
        if (!empty($recommend_relics3)){
            array_push($relicsList,$recommend_relics3);
        }
        $row->recommend_relics= json_encode($relicsList, JSON_UNESCAPED_UNICODE);
        //推荐队伍
        $role = (new GenshinRole())->select()->toArray();
        $roleList = [];
        foreach ($role as $i => $k) {
            $roleList[$k['role_name']] = $k;
        }
        $teamList1 = [];
        $post['team_role1'] = explode(',', $post['team_role1']);
        foreach ($post['team_role1'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList1[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList1)==3){
            array_push($teamList1,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team1 = array('id' => '', 'title' => $post['team_title1'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'), 'teamlist' => $teamList1, 'desc' => '');
        $teamList2 = [];
        $post['team_role2'] = explode(',', $post['team_role2']);
        foreach ($post['team_role2'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList2[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList2)==3){
            array_push($teamList2,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team2 = array('id' => '', 'title' => $post['team_title2'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'),'teamlist' => $teamList2, 'desc' => '');
        $teamList3 = [];
        $post['team_role3'] = explode(',', $post['team_role3']);
        foreach ($post['team_role3'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList3[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList3)==3){
            array_push($teamList3,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team3 = array('id' => '', 'title' => $post['team_title3'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'), 'teamlist' => $teamList3, 'desc' => '');
        $teamList4 = [];
        $post['team_role4'] = explode(',', $post['team_role4']);
        foreach ($post['team_role4'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList4[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList4)==3){
            array_push($teamList4,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team4 = array('id' => '', 'title' => $post['team_title4'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'), 'teamlist' => $teamList4, 'desc' => '');
        $teamList5 = [];
        $post['team_role5'] = explode(',', $post['team_role5']);
        foreach ($post['team_role5'] as $i => $k) {
            if (empty($k)) {
                continue;
            }
            $teamList5[] = array('id' => $roleList[$k]['id'], 'name' => $roleList[$k]['role_name'], 'element' => $roleList[$k]['element'], 'nation' => $roleList[$k]['nation'], 'star' => $roleList[$k]['star']);
        }
        if (sizeof($teamList5)==3){
            array_push($teamList5,array('id' =>'', 'name' =>$post['name'], 'element' => $post['element'], 'nation' => $post['nation'], 'star' => $post['star']));
        }
        $team5 = array('id' => '', 'title' => $post['team_title5'], 'cover' => (new GenshinRole())->where('role_name',$post['team_role1'][0])->value('nation'), 'teamlist' => $teamList5, 'desc' => '');
        $teamList=[];
        if (!empty($team1['teamlist'])){
            array_push($teamList,$team1);
        }
        if (!empty($team2['teamlist'])){
            array_push($teamList,$team2);
        }
        if (!empty($team3['teamlist'])){
            array_push($teamList,$team3);
        }
        if (!empty($team4['teamlist'])){
            array_push($teamList,$team4);
        }
        if (!empty($team5['teamlist'])){
            array_push($teamList,$team5);
        }
        $row->recommend_team= json_encode($teamList, JSON_UNESCAPED_UNICODE);
        //主词条推荐
        $main_entry = [
            array($post['main_entry1']),
            array($post['main_entry2']),
            array($post['main_entry3']),
            array($post['main_entry4']),
            array($post['main_entry5']),
        ];
        $row->main_entry= json_encode($main_entry, JSON_UNESCAPED_UNICODE);
        //副词条推荐
        $sub_entry = [];
        $post['sub_entry'] = explode(',', $post['sub_entry']);
        foreach ($post['sub_entry'] as $i => $k) {
            if (empty($k)){
                continue;
            }
            $sub_entry[] = $k;
        }
        $row->sub_entry = json_encode($sub_entry, JSON_UNESCAPED_UNICODE);
        //加点推荐
        $add_point_recommed = [];
        if (!empty($post['add_point_recommed1'])){
            array_push($add_point_recommed,$post['add_point_recommed1']);
        }
        if (!empty($post['add_point_recommed2'])){
            array_push($add_point_recommed,$post['add_point_recommed2']);
        }
        if (!empty($post['add_point_recommed3'])){
            array_push($add_point_recommed,$post['add_point_recommed3']);
        }
        $row->add_point_recommed = json_encode($add_point_recommed, JSON_UNESCAPED_UNICODE);
        //命座推荐
        $core_mingzuo = [];
        if (!empty($post['core_mingzuo1'])){
            array_push($core_mingzuo,$post['core_mingzuo1']);
        }
        if (!empty($post['core_mingzuo2'])){
            array_push($core_mingzuo,$post['core_mingzuo2']);
        }
        if (!empty($post['core_mingzuo3'])){
            array_push($core_mingzuo,$post['core_mingzuo3']);
        }
        if (!empty($post['core_mingzuo4'])){
            array_push($core_mingzuo,$post['core_mingzuo4']);
        }
        $row->core_mingzuo = json_encode($core_mingzuo, JSON_UNESCAPED_UNICODE);

        //毕业面板
        $row->panel=json_encode(array(
            'sid'=>'','uid'=>'',
            'shengming'=>$post['shengming'],
            'fangyu'=>$post['fangyu'],
            'baoji'=>$post['baoji'],
            'baoshang'=>$post['baoshang'],
            'gongji'=>$post['gongji'],
            'yuansujingtong'=>$post['yuansujingtong'],
            'yuansujiacheng'=>$post['yuansujiacheng'],
            'yuansuchongneng'=>$post['yuansuchongneng'],
            'qjineng'=>$post['skill_level3'],
            'ejineng'=>$post['skill_level2'],
            'pinga'=>$post['skill_level1'],
        ),JSON_UNESCAPED_UNICODE);
        $row->panel_num=json_encode(array(
            'health'=>$post['health'],
            'attack'=>$post['attack'],
            'defense'=>$post['defense'],
            'crit'=>$post['crit'],
            'critDamage'=>$post['critDamage'],
            'elementMastery'=>$post['elementMastery'],
            'elementCharge'=>$post['elementCharge'],
            'elementBonus'=>$post['elementBonus'],
        ),JSON_UNESCAPED_UNICODE);

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}
